package com.atguigu.async;

import com.atguigu.exception.SleepUtils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import java.util.function.Supplier;

public class FutureDemo6 {
    public static void main(String[] args) throws Exception{
        runAsync1();
        System.out.println("main");
        SleepUtils.sleep(3);
    }

    public static void runAsync1() throws ExecutionException, InterruptedException {
        CompletableFuture<String> supplyAsync = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                System.out.println(Thread.currentThread().getName()+"你好supplyAsync");
                SleepUtils.sleep(1);
                return "java0317";
            }
        });
        CompletableFuture<String> thenApply = supplyAsync.thenApply(new Function<String, String>() {
            @Override
            public String apply(String acceptValue) {
                System.out.println(Thread.currentThread().getName() + "第一个接收thenApply返回值" + acceptValue);

                return "thenApply" + acceptValue;
            }
        });

        CompletableFuture<String> thenApply2 = supplyAsync.thenApply(new Function<String, String>() {
            @Override
            public String apply(String acceptValue) {
                System.out.println(Thread.currentThread().getName() + "第二个接收thenApply2返回值" + acceptValue);

                return "thenApply2" + acceptValue;
            }
        });
        System.out.println(thenApply.get());
        System.out.println(thenApply2.get());
    }

}
